package com.jcl.recruit.basic.register.impl;

import com.jcl.core.utils.EncodeMd;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.basic.register.model.RegisterEnum;
import com.jcl.recruit.basic.register.model.RegisterResult;
import com.jcl.recruit.basic.register.vo.RegisterVo;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.depart.dao.DepartmentDao;
import com.jcl.recruit.org.model.Department;
import com.jcl.recruit.user.dao.CorpUserDao;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * @ClassName CorpRecruitRegister
 * @Author yyliu
 * @Date 2018/12/17 10:42
 * @Version 1.0
 **/
@Component
public class UserRecruitRegister {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    protected CorpUserDao corpUserDao;

    private RegisterVo registerVo;

    public RegisterResult register(String corpCode,
                                   String password,
                                   RegisterEnum registerEnum) {
        RegisterResult result = null;
        Integer departId = registerVo.getDepartId();
        if (null == departId) {
            Department department = departmentDao.findRootByCorpCode(corpCode,
                    Constants.DepartmentInfo.ROOT_SUPER_ID);
            departId = null != department ? department.getId() : null;
        }

        User user = this.registerUser(corpCode, departId, password, false);
        result.setCorpCode(corpCode);
        result.setUserId(user.getId());
        return result;
    }


    public User registerUser(String corpCode,
                             Integer departId,
                             String password,
                             boolean isAdmin) {
        User user = new User();
        user.setCorpCode(corpCode);
        user.setEmail(registerVo.getEmail());
        String userName = null;
        if (StringUtils.hasLength(registerVo.getUsername())) {
            userName = registerVo.getUsername();
        } else {
            if (null != registerVo.getUserAccess()) {
                userName = registerVo.getUserAccess().getNickname();
            }
        }
        user.setRealname(userName);
        user.setUsername(userName);
        user.setPostName(registerVo.getUserBusiness());
        try {
            user.setPassword(EncodeMd.EncodeByMd5(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        user.setDepartId(departId);
        user.setHr(true);
        if (isAdmin) {
            // 默认 注册有效
            user.setStatus(Constants.UserInfo.USER_STATUS_ACTIVE);
            // 默认管理员
            user.setRoles(Constants.UserInfo.createAdminAndHrRole());
        } else {
            // 默认 注册无效
            user.setStatus(Constants.UserInfo.USER_STATUS_UNACTIVE);
            // 默认HR
            user.setRoles(Constants.UserInfo.createHrRole());
        }
        user.setInviteUserId(registerVo.getInviteUserId());
        user.setCreateTime(new Date());
        corpUserDao.insertSelective(user);
        return user;
    }

    public ConsoleUserAccess registerUserAccess(String corpCode, Integer departId, boolean isAdmin) {

        return null;
    }

    public RegisterVo getRegisterVo() {
        return registerVo;
    }

    public void setRegisterVo(RegisterVo registerVo) {
        this.registerVo = registerVo;
    }
}
